<?php
include '/var/www/html/new/com.inc.php';

if(!isset($_SESSION['sid'])) exit('{"status":"fail","msg":"登陆超时","code":0}');

$ac = $GLOBALS['_ac'];
$pw = $GLOBALS['_pw'];
$s  = new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));

$memberSID = $_SESSION['sid'];

switch(trim($_REQUEST['action'])){
	
	// 获取分终端列表
	case 'sub_terminal_list':
		$terminal = $_REQUEST['terminal'] + 0;
		if($terminal==0) exit('{"status":"fail","msg":"参数错误"}');
		$sql = "select id,preTerminal,nowSalerID from pft_terminal_share where nowTerminal=$terminal and privID=$memberSID";
		if($memberSID==1) $sql = "select id,preTerminal,nowSalerID from pft_terminal_share where nowTerminal=$terminal";
		$GLOBALS['le']->query($sql);
		while($row = $GLOBALS['le']->fetch_assoc()) $tlist[] = $row;
		exit(json_encode(array('status'=>'success','list'=>$tlist)));
		break;
	// 添加分终端
	case 'sub_terminal_add':
	
		$landid   = $_REQUEST['landid'] + 0;
		$sub_tnum = $_REQUEST['num'] + 0;
		$terminal = $_REQUEST['terminal'] + 0;

		$params = array();
		$params['mid']       = $_SESSION['sid'];
		$params['action']    = 'TERMINAL_DIVIDE';
		$params['main_id']   = $landid;
		$params['merge_num'] = 1;
		// $params['merge_num'] = $sub_tnum;
		
		ksort($params);
		$auth = md5(implode(',', $params));
		$params['auth'] = $auth;
		$clsResult = file_get_contents('http://10.160.4.140/new/d/api/terminal_merge.php?'.http_build_query($params));
		$resResult = json_decode($clsResult, true);
		if($resResult['status']=='ok') exit(json_encode(array('status'=>'success','list'=>$resResult['terminals'])));
		// exit(json_encode(array('status'=>'success','list'=>$resResult['terminals'])));
		// exit('{"status":"success","list":[{"terminal_id":"6279","is_main":1},{"terminal_id":11527,"is_main":1}]}');
		exit('{"status":"fail","msg":"其他错误，请重试"}');
		break;
	// 删除分终端
	case 'sub_terminal_del':
		// file_get_contents('http://10.160.4.140/new/d/api/terminal_merge.php?'.http_build_query($params));
		exit('{"status":"success","msg":"删除成功"}');
		break;
	default:exit('{"status":"fail","msg":"参数错误"}');break;
}